package com.itheima.controller;


import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {


    //private static final Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping("/depts")
    public Result list(){
        //System.out.println("查询全部部门数据");
        log.info("查询全部部门数据");
        List<Dept> deptList=deptService.findAll();
        return Result.success(deptList);
    }

    /**
     * 删除部门 方式一：使用HttpServletRequest
     */
/*//  /*  @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idStr=request.getParameter("id");
//        Integer id=Integer.parseInt(idStr);
//        System.out.println("根据ID删除部门"+ id);
//        return Result.success();
       }*/


    /**
     * 删除部门 方式二：使用@RequestParam
     */
    /*@DeleteMapping("/depts")
    public Result delete(@RequestParam("id") Integer deptId){
        System.out.println("根据ID删除部门"+ deptId);
        return Result.success();
    }*/

    /**
     * 删除部门 方式三：省略，名臣一致
     */
    @DeleteMapping("/depts")
    public Result delete(Integer id){
        //System.out.println("根据ID删除部门"+ id);
        log.info("根据ID删除部门:{}",id);
        deptService.deleteById(id);
        return Result.success();
    }


    /**
     * 新增部门
     */

    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        //System.out.println("新增部门"+dept);
        log.info("新增部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据ID查询部门
     */
    @GetMapping("/depts/{id}")
    public Result getInfo(@PathVariable Integer id){
        //System.out.println("查询部门ID为"+id);
        log.info("查询部门ID为:{}",id);
        Dept dept=deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     */
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门:"+dept);
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
